package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.BusinessQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.FileUploadUtil;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;


@RequestMapping("/business")
@Controller
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @Autowired
    ServletContext servletContext;

    @RequiredPermission(name = "门店页面", expression = "business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessQueryObject qo) {
        PageInfo<Business> pageInfo = businessService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/business/list";
    }


    @RequiredPermission(name = "门店删除", expression = "business:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        businessService.deleteByPrimaryKey(id);

        return "redirect:/business/list";
    }

    @RequiredPermission(name = "门店新增/编辑", expression = "business:savrOrUpdate")
    @RequestMapping("/input")
    public String input(Model model, Business business) {
        if (business.getId() != null) {
            Business business1 = businessService.selectByPrimaryKey(business.getId());
            model.addAttribute("business", business1);
        }
        return "/business/input";
    }

    @RequiredPermission(name = "门店新增/编辑", expression = "business:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file) throws Exception {
        /**
         * 对附件的操作
         */
        String realPath = servletContext.getRealPath("/");
        //1.判断是否有附件提交
        if (file != null && file.getSize() > 0) {
            //2.有则删除已经存在的附件
            if (StringUtils.hasText(business.getLicenseImg())) {
                FileUploadUtil.deleteFile(realPath + business.getLicenseImg());
            }
            //3.webapp所在的绝对路径
            //String path = servletContext.getRealPath("/");
            //4.获取文件的绝对路径
            String url = FileUploadUtil.uploadFile(file, realPath);
            //4.将文件保存到数据库中
            business.setLicenseImg(url);
        }
        if (business.getId() != null) {
            businessService.updateByPrimaryKey(business);
        } else {
            businessService.insert(business);
        }

        return "redirect:/business/list";
    }


    @RequestMapping("/index")
    public String index(Model model){
        return "/index";
    }
}

